// 失物招领相关的接口
import request from '@/utils/request'
import type {
  PublishLostItemForm,
  PublishLostItemResponse,
  LostItemQueryParams,
  LostItemListResponse,
  LostItemDetailResponse,
  ClaimForm,
  ClaimResponse,
  LikeResponse,
  CommentForm,
  CommentResponse,
  ReplyForm,
  CommentListResponse,
  UploadImageResponse
} from './type'

// 统一管理接口
enum Api {
  // 失物相关
  PUBLISH_ITEM = '/lost/publish',
  ITEM_LIST = '/lost/list',
  ITEM_DETAIL = '/lost/detail',
  UPDATE_ITEM = '/lost/update',
  DELETE_ITEM = '/lost/delete',
  
  // 申领相关
  CLAIM_ITEM = '/lost/claim',
  CLAIM_LIST = '/lost/claim/list',
  PENDING_CLAIMS = '/lost/claims/pending',
  AUDIT_CLAIM = '/lost/claims/audit',
  
  // 点赞相关
  LIKE_ITEM = '/lost/like',
  UNLIKE_ITEM = '/lost/unlike',
  LIKE_COMMENT = '/lost/comment/like',
  UNLIKE_COMMENT = '/lost/comment/unlike',
  LIKE_REPLY = '/lost/reply/like',
  UNLIKE_REPLY = '/lost/reply/unlike',
  
  // 评论相关
  COMMENT_LIST = '/lost/comment/list',
  ADD_COMMENT = '/lost/comment/add',
  DELETE_COMMENT = '/lost/comment/delete',
  ADD_REPLY = '/lost/reply/add',
  DELETE_REPLY = '/lost/reply/delete',
  
  // 图片上传
  UPLOAD_IMAGE = '/lost/upload/image',
}

// ============== 失物相关接口 ==============

// 发布失物
export const reqPublishLostItem = (data: FormData) => 
  request.post<any, PublishLostItemResponse>(Api.PUBLISH_ITEM, data, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })

// 获取失物列表
export const reqLostItemList = (params: LostItemQueryParams) => 
  request.get<any, LostItemListResponse>(Api.ITEM_LIST, { params })

// 获取失物详情
export const reqLostItemDetail = (id: number) => 
  request.get<any, LostItemDetailResponse>(`${Api.ITEM_DETAIL}/${id}`)

// 更新失物信息
export const reqUpdateLostItem = (id: number, data: FormData) => 
  request.put<any, PublishLostItemResponse>(`${Api.UPDATE_ITEM}/${id}`, data, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })

// 删除失物
export const reqDeleteLostItem = (id: number) => 
  request.delete<any, any>(`${Api.DELETE_ITEM}/${id}`)

// ============== 申领相关接口 ==============

// 申领失物
export const reqClaimItem = (data: ClaimForm) => 
  request.post<any, ClaimResponse>(Api.CLAIM_ITEM, data)

// 获取申领列表（管理员）
export const reqClaimList = (itemId: number) => 
  request.get<any, any>(`${Api.CLAIM_LIST}/${itemId}`)

// 获取待审核申领列表（管理员）
export const reqGetPendingClaims = (params: { page: number; pageSize: number }) =>
  request.get<any, any>(Api.PENDING_CLAIMS, { params })

// 审核申领（管理员）
export const reqAuditClaim = (data: { claimId: number; status: number; auditReason?: string }) =>
  request.post<any, any>(Api.AUDIT_CLAIM, data)

// ============== 点赞相关接口 ==============

// 点赞失物
export const reqLikeItem = (itemId: number) => 
  request.post<any, LikeResponse>(`${Api.LIKE_ITEM}/${itemId}`)

// 取消点赞失物
export const reqUnlikeItem = (itemId: number) => 
  request.delete<any, LikeResponse>(`${Api.UNLIKE_ITEM}/${itemId}`)

// 点赞评论
export const reqLikeComment = (commentId: number) => 
  request.post<any, LikeResponse>(`${Api.LIKE_COMMENT}/${commentId}`)

// 取消点赞评论
export const reqUnlikeComment = (commentId: number) => 
  request.delete<any, LikeResponse>(`${Api.UNLIKE_COMMENT}/${commentId}`)

// 点赞回复
export const reqLikeReply = (replyId: number) => 
  request.post<any, LikeResponse>(`${Api.LIKE_REPLY}/${replyId}`)

// 取消点赞回复
export const reqUnlikeReply = (replyId: number) => 
  request.delete<any, LikeResponse>(`${Api.UNLIKE_REPLY}/${replyId}`)

// ============== 评论相关接口 ==============

// 获取评论列表
export const reqCommentList = (itemId: number, page: number = 1, pageSize: number = 10) => 
  request.get<any, CommentListResponse>(Api.COMMENT_LIST, {
    params: { itemId, page, pageSize }
  })

// 添加评论
export const reqAddComment = (data: CommentForm) => 
  request.post<any, CommentResponse>(Api.ADD_COMMENT, data)

// 删除评论
export const reqDeleteComment = (commentId: number) => 
  request.delete<any, any>(`${Api.DELETE_COMMENT}/${commentId}`)

// 添加回复
export const reqAddReply = (data: ReplyForm) => 
  request.post<any, CommentResponse>(Api.ADD_REPLY, data)

// 删除回复
export const reqDeleteReply = (replyId: number) => 
  request.delete<any, any>(`${Api.DELETE_REPLY}/${replyId}`)

// ============== 图片上传接口 ==============

// 上传图片
export const reqUploadImage = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return request.post<any, UploadImageResponse>(Api.UPLOAD_IMAGE, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

